/**
 * Copyright (c) 2017 ShopJsp. All Rights Reserved.
 * ============================================================================
 * 版权所有 2011 - 今 北京华宇盈通科技有限公司，并保留所有权利。
 * ----------------------------------------------------------------------------
 * 提示：在未取得SHOPJSP商业授权之前，您不能将本软件应用于商业用途，否则SHOPJSP将保留追究的权力。
 * ----------------------------------------------------------------------------
 * 官方网站：http://www.shopjsp.com
 * ============================================================================
 * @author 张丁方
 * @CreateTime 2017-3-29 15:22:14
 */
package com.hyyt.shopjsp.product.service.imp;

import com.hyyt.shopjsp.product.dao.ShopProductParameterDAO;
import com.hyyt.shopjsp.product.pojo.ShopProductParameter;
import com.hyyt.shopjsp.product.pojo.ShopProductParameterValue;
import com.hyyt.shopjsp.product.service.IShopProductParameterService;
import com.hyyt.shopjsp.product.service.IShopProductParameterValueService;
import com.hyyt.shopjsp.util.common.Utils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Service
public class ShopProductParameterService implements IShopProductParameterService {
    private static  final Logger logger = Logger.getLogger(IShopProductParameterService.class);

    @Resource
    private ShopProductParameterDAO shopProductParameterDAO;
    @Resource
    private IShopProductParameterValueService shopProductParameterValueService;

    /**
     * 保存SHOP_商品参数
     *
     * @param shopProductParameter SHOP_商品参数
     * @return 保存后的SHOP_商品参数
     */
    @Override
    public void saveShopProductParameter(ShopProductParameter shopProductParameter) {
        shopProductParameterDAO.insert(shopProductParameter);
        List<ShopProductParameterValue> shopProductParameterValueList = shopProductParameter.getShopProductParameterValueList();
        if(Utils.collectionIsNotEmpty(shopProductParameterValueList)){
            for(ShopProductParameterValue shopProductParameterValue:shopProductParameterValueList){
                if(Utils.objectIsNotEmpty(shopProductParameterValue.getParamValueName()) && Utils.objectIsNotEmpty(shopProductParameterValue.getSort())){
                    shopProductParameterValue.setParamId(shopProductParameter.getParamId());
                    shopProductParameterValueService.saveShopProductParameterValue(shopProductParameterValue);
                }
            }
        }
    }

    /**
     * 更新SHOP_商品参数
     *
     * @param shopProductParameter SHOP_商品参数
     * @return 更新后的SHOP_商品参数
     */
    @Override
    public void updateShopProductParameter(ShopProductParameter shopProductParameter) {
        shopProductParameterDAO.update(shopProductParameter);
        List<ShopProductParameterValue> shopProductParameterValueList = shopProductParameter.getShopProductParameterValueList();
        if(Utils.collectionIsNotEmpty(shopProductParameterValueList)){
            for(ShopProductParameterValue shopProductParameterValue:shopProductParameterValueList){
                if(Utils.objectIsNotEmpty(shopProductParameterValue.getSort()) && Utils.objectIsNotEmpty(shopProductParameterValue.getParamValueName())){
                    shopProductParameterValue.setParamId(shopProductParameter.getParamId());
                    if(Utils.objectIsEmpty(shopProductParameterValue.getParamValueId())){
                        shopProductParameterValueService.saveShopProductParameterValue(shopProductParameterValue);
                    }else {
                        shopProductParameterValueService.updateShopProductParameterValue(shopProductParameterValue);
                    }
                }
            }
        }
    }

    /**
     * 删除指定ID的SHOP_商品参数
     *
     * @param id SHOP_商品参数ID
     */
    @Override
    public void deleteShopProductParameter(Integer id) {
        shopProductParameterDAO.delete(id);
    }

    /**
     * 删除指定ID集合的SHOP_商品参数
     *
     * @param ids SHOP_商品参数ID集合
    */
    public void deleteShopProductParameter(Collection<Integer> ids){
        shopProductParameterDAO.batchDelete(ids);
    }

    /**
     * 根据ID加载一个SHOP_商品参数
     *
     * @param id SHOP_商品参数ID
     * @return SHOP_商品参数
     */
    @Override
    public ShopProductParameter loadShopProductParameter(Integer id) {
        return shopProductParameterDAO.load(id);
    }

    /**
    * 查询所有的SHOP_商品参数，通常有最多取30000条记录的限制
    *
    * @return 所有的SHOP_商品参数，通常有最多取30000条记录的限制
    */
    @Override
    public List<ShopProductParameter> queryAllShopProductParameter() {
        return shopProductParameterDAO.queryAll();
    }

    /**
     * 查询SHOP_商品参数
     *
     * @param params 查询参数，为key-value形式
     * @return SHOP_商品参数集合
     */
    @Override
    public List<ShopProductParameter> queryShopProductParameter(Map<String, Object> params) {
        return shopProductParameterDAO.query(params);
    }

    /**
     * 查询SHOP_商品参数
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品参数集合
     */
    @Override
    public List<ShopProductParameter> queryShopProductParameter(Map<String, Object> params, int startRow, int rowSize) {
        return shopProductParameterDAO.query(params, startRow, rowSize);
    }

    /**
     * 根据商品分类ID查询SHOP_商品参数
     *
     * @param params   查询参数，没有则传null,分页参数key为“sortColumns”
     * @param startRow 起始行数（不含起始行的数据）
     * @param rowSize  要查询记录数
     * @return SHOP_商品参数集合
     */
    @Override
    public List<Map<String,Object>> queryProductParameterByProductTypeId(Map<String, Object> params, int startRow, int rowSize) {
        return shopProductParameterDAO.query("queryProductParameterByProductTypeId",params, startRow, rowSize);
    }

    /**
     * 统计SHOP_商品参数
     *
     * @param params 查询参数，没有则传null,分页参数key为“sortColumns”
     * @return 结果集记录数
     */
    @Override
    public int countShopProductParameter(Map<String, Object> params) {
        return shopProductParameterDAO.count(params);
    }

}